﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;

using SBPweb.Modules.Modularity;
using SBPweb.Modules.Smartparts;

using SBPweb.MediaCenter.Tools.WPF.Controls;
using SBPweb.MediaCenter.Tools.WPF.Extenders;

using SBPweb.MediaCenter.Common.Services;

namespace SBPweb.MediaCenter.Presentation.WPF.Controls
{

    public class CarouselMenuView : CarouselListViewer<CarouselMenu>
    {

        #region Private fields

        private IModuleHost moduleHost;
        private Dictionary<IMenuItem, IModule> modulesDict = new Dictionary<IMenuItem, IModule>();

        #endregion

        #region Constructor logic

        public CarouselMenuView(IModuleHost moduleHost, ModelVisual3D visual, Camera camera)
            : base(visual, camera)
        {
            this.moduleHost = moduleHost;
            List.ItemAdded += new EventHandler<MenuEventArgs>(List_ItemAdded);
            List.ItemRemoved += new EventHandler<MenuEventArgs>(List_ItemRemoved);
            base.SelectedIndexChanged += new EventHandler(CarouselMenuView_SelectedIndexChanged);
        }

        #endregion

        #region Property accessors

        protected ILayoutService LayoutService
        {
            get
            {
                return moduleHost.ModuleLoader.Services.Get<ILayoutService>();
            }
        }

        #endregion

        #region Event handlers

        private void List_ItemAdded(object sender, MenuEventArgs e)
        {
            IMenuItem menu = e.Menu;
            menu.Selected += new EventHandler<MenuEventArgs>(menu_Selected);
            menu.Pressed += new EventHandler<MenuEventArgs>(menu_Pressed);
            if (menu is UserControl)
            {
                base.Add(menu as UserControl);
            }
            if (menu is Visual3D)
            {
                base.Add(menu as Visual3D);
            }
            if (e.Menu is DependencyObject)
            {
                TagDictionary tag = TagDictionary.GetTag(e.Menu as DependencyObject);
                if (tag != null && tag.ContainsKey("ParentModule"))
                {
                    IModule module = tag.Get<string, IModule>("ParentModule");
                    modulesDict.Add(e.Menu, module);
                }
            }
        }

        private void List_ItemRemoved(object sender, MenuEventArgs e)
        {
            IMenuItem menu = e.Menu;
            menu.Selected -= new EventHandler<MenuEventArgs>(menu_Selected);
            menu.Pressed -= new EventHandler<MenuEventArgs>(menu_Pressed);
            if (menu is UserControl)
            {
                base.Remove(menu as UserControl);
            }
            if (menu is Visual3D)
            {
                base.Remove(menu as Visual3D);
            }
        }

        private void CarouselMenuView_SelectedIndexChanged(object sender, EventArgs e)
        {
            IMenuItem menu = List[SelectedIndex];
            if (menu is UserControl)
            {
                (menu as UserControl).Focus();
            }
            menu.Select();
        }

        private void menu_Selected(object sender, MenuEventArgs e)
        {
            foreach (KeyValuePair<IMenuItem, IModule> kvp in modulesDict)
            {
                if (kvp.Key == e.Menu)
                {
                    LayoutService.BringToFront(new List<ISmartpart>(kvp.Value.Smartparts));
                }
                else
                {
                    LayoutService.SendToBack(new List<ISmartpart>(kvp.Value.Smartparts));
                }
            }
        }

        private void menu_Pressed(object sender, MenuEventArgs e)
        {
        
        }

        #endregion
    }

}